package com.ag.wzp.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.ag.bean.TbConnectLog;
import com.ag.bean.TbContact;
import com.ag.bean.TbContactLog;
import com.ag.bean.TbEvent;
import com.ag.common.IBaseService;
import com.ag.common.IDUtil;
import com.ag.common.IIDService;
import com.ag.common.KeyUtil;
import com.ag.dao.TbDeptDAO;
import com.ag.dao.TbDisposalAdviceDAO;
import com.ag.dao.TbEventLevelDAO;
import com.ag.dao.TbEventStatusDAO;
import com.ag.wzp.dao.WzpTbContactLogDAO;
import com.ag.wzp.dao.WzpTbEventDAO;

@Service("eventServiceImpl")
public class EventServiceImpl implements IBaseService<TbEvent, String> {
	private static Logger logger = Logger.getLogger(EventServiceImpl.class);
	@Resource(name = "iDServiceImpl")
	private IIDService idServiceImpl;
	@Resource(name = "WzpTbEventDAO")
	WzpTbEventDAO eventDAO;
	@Resource(name = "TbEventStatusDAO")
	TbEventStatusDAO eventStatusDAO;
	@Resource(name = "TbEventLevelDAO")
	TbEventLevelDAO eventLevelDAO;
//	@Resource(name = "TbNotifytodeptDAO")
//	TbNotifytodeptDAO submitDAO;
	@Resource(name="TbDeptDAO")
	TbDeptDAO deptDAO;
	@Resource(name="TbDisposalAdviceDAO")
	TbDisposalAdviceDAO tbDisposalAdviceDAO;
	@Resource(name="WzpTbContactLogDAO")
	WzpTbContactLogDAO wzpTbContactLogDAO;
	
	@Override
	public boolean delete(String id) {
		try {
			TbEvent e = this.eventDAO.findById(id);
			e.setA(IDUtil.DELETE);
			eventDAO.attachDirty(e);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public boolean delete(TbEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean save(TbEvent e) {
		try {
			String eid = this.idServiceImpl.findTablePK(IDUtil.TB_EVENT);
			e.setEId(KeyUtil.getEventId() + eid);
			e.setEAcceptTime(new Date());
			e.setA(IDUtil.PRESERVE);
			this.eventDAO.save(e);
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}
	
	@Override
	public boolean update(TbEvent e) {
		logger.info("EId=" + e.getEId());
		try {
			TbEvent oldEvent = this.eventDAO.findById(e.getEId());
			oldEvent.setTbEventStatus(e.getTbEventStatus());
			oldEvent.setTbEventLevel(e.getTbEventLevel());
			oldEvent.setTbEventSource(e.getTbEventSource());
			oldEvent.setESubmiterContact(e.getESubmiterContact());
			oldEvent.setESubmiter(e.getESubmiter());
			oldEvent.setEEventAddr(e.getEEventAddr());
			oldEvent.setELatitude(e.getELatitude());
			oldEvent.setELongitude(e.getELongitude());
			oldEvent.setEEventtime(e.getEEventtime());
			oldEvent.setEEventname(e.getEEventname());
			oldEvent.setTbEventType(e.getTbEventType());
			oldEvent.setEDeaths(e.getEDeaths());
			oldEvent.setEInjuries(e.getEInjuries());
			oldEvent.setEEventCause(e.getEEventCause());
			oldEvent.setEEventDesc(e.getEEventDesc());
			oldEvent.setETakeMeasure(e.getETakeMeasure());
			//e.setEAcceptTime(oldEvent.getEAcceptTime());
			//e.setESubmitTime(oldEvent.getESubmitTime());
			//e.setA(oldEvent.getA());
			this.eventDAO.attachDirty(oldEvent);
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}
	
	public boolean update(String EId, String tbesId) {
		TbEvent event = this.eventDAO.findById(EId);
		event.getTbEventStatus().setTbesId(tbesId);
		this.eventDAO.attachDirty(event);
		return false;
	}
	
	public boolean isNotify(String E_id){
		List<TbContactLog> connectLog = wzpTbContactLogDAO.findByC(E_id);
		System.out.println(E_id);
		for(int i =0;i<10;i++)
			System.out.println("&&");
		System.out.println(connectLog);
		if(connectLog.size()>=1)
			return true ;
		else return false;
	}
//	public boolean saveSubmit(String eId, String elId, String selDept) {
//		logger.info("EId ="+eId);
//		TbEvent event = this.eventDAO.findById(eId);
//		event.setTbEventLevel(this.eventLevelDAO.findById(elId));
//		this.eventDAO.attachDirty(event);
//		logger.info("EId = " + event.getEId());
//		String[] depts = selDept.split(",");
//		for (String d : depts) {
//			if (d.length() > 0) {
//				TbSubmit ts = new TbSubmit();
//				ts.setSId(eId + d);
//				ts.setTbEvent(event);
//				logger.info("dept="+d);
//				ts.setTbDept(this.deptDAO.findById(d));
//				this.submitDAO.save(ts);
//			}
//		}
//		return false;
//	}

	@Override
	public TbEvent findById(String id) {
		return this.eventDAO.findById(id);
	}

}
